package com.xinyiweidai.xinyiweidai.module.bind;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Button;

import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.ContactsBean;
import com.xinyiweidai.xinyiweidai.util.SpUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * class:   ContactsActivity
 * data：   2018/9/12  14:20
 * author:  LanX
 * Description: 通讯录认证
 */
public class ContactsActivity extends BaseActivity {


    private List<ContactsBean> contactsList = new ArrayList<>();
    private Button btn_contacts;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_contacts;
    }


    @Override
    protected void initToolBar() {
        new MyTitle(this, R.drawable.ic_back, "通讯录认证", 0, true);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        btn_contacts = (Button) findViewById(R.id.btn_contacts);
    }


    @Override
    protected void addClickEvent() {
        btn_contacts.setOnClickListener(view -> judgePermission());
    }


    private void judgePermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
        } else {
            getContacts();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    getContacts();
                } else {
                    MyToast.showError(ContactsActivity.this, "认证失败");
                }
                break;
            default:
        }
    }


    //读取通讯录
    private void getContacts() {
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactsList.add(new ContactsBean(SpUtil.getInstance().getUserInfo().getUser_id(), name, number,"0"));
                }
            }

            if (contactsList.size() == 0) {
                MyToast.showError(ContactsActivity.this, "您的通讯录为空?认证失败!");
            } else {
                Intent intent=new Intent(this,ContactsListActivity.class);
                intent.putExtra("contacts_data", (Serializable) contactsList);
                startActivity(intent);
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}
